<?php
Yii::import('system.web.widgets.CActiveForm');
class CActiveFormPlus extends CActiveForm{
    public $templates=array();
	public function init(){
        $this->htmlOptions['class'] = 'form-horizontal';
        parent::init();
        $this->renderTemplate();
    }
	
    protected  function renderTemplate(){
    	if(!empty($this->templates)){    	    
    		foreach ($this->templates as $templateName=>$template){
    			$this->getController()->renderPartial($template['view'],array(
    					'model'=>$this->dataProvider,
    					'form' =>$this
    			));
    		}
    	}
    }
    
    public function formPart($model,$view,$variable = array()){
        $data['model'] = $model;
        $data['form']   = $this;
        return $this->getController()->renderPartial($view,$data);
    }
    
    public function formParts($model,$parts){
        foreach ($parts as $part){
            if (!array_key_exists('variable', $part)){
                $part['variable'] = array();
            }
            $this->formPart($model,$part['view'],$part['variable']);
        }
        return $this;
    }
    
    public function errorSummary($models,$header=null,$footer=null,$htmlOptions=array()){
        $this->errorMessageCssClass .= ' hide';
        $htmlOptions['class'] = 'alert alert-danger alert-dismissible errors';
        return parent::errorSummary($models,$header,$footer,$htmlOptions);
    }
   
    public function textFieldCAutoComplete($model,$attribute,$options,$htmlOptions=array()){
        $htmlName = get_class($model).'['.$attribute.']';
        $htmlOptions['attribute'] = $attribute;
        $source = array();
        if (!array_key_exists('source', $options)){
            
            if (array_key_exists('dataProvider',$options)){
                $arClass = Yii::import($options['dataProvider']['class']);
                $ar = new $arClass();
                $temp = $ar->findAll();
                $sourceAttribute = $options['dataProvider']['attribute'];
                if (!empty($temp)){
                    foreach ($temp as $data){
                        array_push($source, $data->$sourceAttribute);
                    }
                }
            }
            
        }else{
            $source = $options['source'];
        }
        
        $config =   array(
            'name'=>$htmlName,
            'source'=>$source,
            'htmlOptions'=>$htmlOptions,
            'value'     => $model->$attribute
        );
        if (array_key_exists('options', $options)){
            $config['options'] = $options['options'];
        }
        $formOptions = CJavaScript::encode(array(
            'addSetValue' => array(
                $attribute => 'js:function(value,element){element.val(value);}',
            ),
            'addGetValue' => array(
                $attribute => 'js:function(value,element){
                    return element.val();
                }',
            ),
        ));
        $this->buildCwidgetCActiveFormPlus($attribute,"
            $('#{$this->getId()}').cwidgetCActiveFormPlus({$formOptions});
        ");
        $this->widget('zii.widgets.jui.CJuiAutoComplete',$config);
    }
    
    public function textField($model,$attribute,$htmlOptions=array()){
        $htmlOptions['attribute'] = $attribute;
        $formOptions = CJavaScript::encode(array(
            'addSetValue' => array(
                $attribute => 'js:function(value,element){element.val(value);}',
            ),
            'addGetValue' => array(
                $attribute => 'js:function(value,element){
                    return element.val();
                }',
            ),
        ));
        $this->buildCwidgetCActiveFormPlus($attribute,"
            $('#{$this->getId()}').cwidgetCActiveFormPlus({$formOptions});
        ");
        return parent::textField($model, $attribute,$htmlOptions);
    }
    
    public function textArea($model,$attribute,$htmlOptions=array()){
        $htmlOptions['attribute'] = $attribute;
        $formOptions = CJavaScript::encode(array(
            'addSetValue' => array(
                $attribute => 'js:function(value,element){element.val(value);}',
            ),
            'addGetValue' => array(
                $attribute => 'js:function(value,element){return element.val();}',
            ),
        ));
        $this->buildCwidgetCActiveFormPlus($attribute,"
            $('#{$this->getId()}').cwidgetCActiveFormPlus({$formOptions});
        ");
        return parent::textArea($model, $attribute,$htmlOptions);
    }
    
    public function textAreaEditor($model,$attribute,$config = array()){
        $config['model'] = $model;
        $config['attribute'] = $attribute;
        $config['attr']['attribute'] = $attribute;
        $config['attr']['id'] = $attribute;
        
        $formOptions = CJavaScript::encode(array(
            'addSetValue' => array(
                $attribute => 'js:function(value,element){
                    var CKEDID = element.attr("id");
                    CKEDITOR.instances[CKEDID].setData(value);
                }',
            ),
            'addGetValue' => array(
                $attribute => 'js:function(value,element){
                    var CKEDID = element.attr("id");
                    return CKEDITOR.instances[CKEDID].getData();
                }',
            ),
        ));
        $this->buildCwidgetCActiveFormPlus($attribute,"
            $('#{$this->getId()}').cwidgetCActiveFormPlus({$formOptions});
        ");
        
        
        return $this->widget('ext.editor.CKkceditor',$config,true);
    }
    
    
    public function dateTimeFieldJs($model,$attribute,$options=array(),$htmlOptions=array()){
        $html = null;
        $htmlOptions['class'] = ' form-control-date-time '.$htmlOptions['class'];
        $id = $this->getId().'-'.$attribute.'-date-time';
        //        application.components.widgets.CJuiDateTimePickerPlus
        $html = $this->widget('ext.CJuiDateTimePicker.CJuiDateTimePicker', array(
            'id' => $id,
            'model' => $model,
            'attribute' =>$attribute,
            'options'=>$options,
            'htmlOptions'=>$htmlOptions,
            'language'=>'en',
        ),true);
        $html .= CHtml::label('',$id,array('class' => 'glyphicon glyphicon-calendar form-control-date-tim-ico'));
        return $html;
    }
    
    public function hiddenField($model,$attribute,$htmlOptions=array()){
        $htmlOptions['attribute'] = $attribute;
        return parent::hiddenField($model, $attribute,$htmlOptions);
    }

    public function buildCwidgetCActiveFormPlus($attribute,$attributeJs){
		$cs=Yii::app()->getClientScript();
		$cs->registerCoreScript('jquery');
		$cs->registerScriptFile(Yii::app()->resource->path('js').'/widgets/jquery.cActiveFormPlus.js',CClientScript::POS_END);
		Yii::app()->clientScript->registerScript('cActiveFormPlus-'.$this->getId().'-'.$attribute,$attributeJs,CClientScript::POS_END);
    }
    
}